﻿@using BootstrapBlazor.Components
@using Microsoft.AspNetCore.SignalR.Client
@using TrackSystem.Entities.Extensions
@using TrackSystem.IO.Formula
@using TrackSystem.IO.Models
@using TrackSystem.IO.Sensors

<body class="bg-gradient-to-br from-blue-50 to-purple-100 min-h-screen flex items-center justify-center p-4">
    <div class="main-container">
        <div class="column">
            <div class="column-content">
                <h3 class="text-xl font-semibold mb-2"> 通信传感器  @snr.Name</h3>
                <div class="col-6 col-sm-4 col-md-6 col-lg-6">
                    <Switch OnColor="Color.Success" OnText="通信使能" OffText="通信关闭"
                            @bind-value="@snr.Enabled"
                            OnValueChanged="@((a) => SetValue(snr.Name??="", a))" />
                </div>
                <p class="text-gray-700">  读/写成功次数: @snr.Succ_R / @snr.Succ_W</p>
                <p class="text-gray-700">读/写失败次数: @snr.Fail_R / @snr.Fail_W</p>
                <p class="text-gray-700">缓冲队列长度: @snr.QueueLen  通信耗时: @snr.Lapse ms </p>
                @if (snr.Tx_r.Length > 0)
                {
                    <p class="text-gray-700">读取发送帧: @ByteArrayToHexString(snr.Tx_r) </p>
                }
                @if (snr.Rx_r.Length > 0)
                {
                    <p class="text-gray-700">读取接收帧: @ByteArrayToHexString(snr.Rx_r) </p>
                }
                @if (snr.Tx_w.Length > 0)
                {
                    <p class="text-gray-700">写入发送帧: @ByteArrayToHexString(snr.Tx_w) </p>
                }
                @if (snr.Rx_w.Length > 0)
                {
                    <p class="text-gray-700">写入接收帧: @ByteArrayToHexString(snr.Rx_w) </p>
                }
            </div>
        </div>

        <div class="column">
            <div class="column-content">
                <h3 class="text-xl font-semibold mb-2">只读寄存器</h3>
                @if (snr.roRegs is not null)
                {
                    @foreach (var reg in snr.roRegs)
                    {
                        <p class="text-gray-700"> @reg.Key : @reg.Value</p>
                    }
                } 
            </div>
        </div>

        <div class="column">
            <div class="column-content">
                <h3 class="text-xl font-semibold mb-2">读写寄存器</h3>
                @if (snr.rwRegs is not null)
                {
                    @foreach (var reg in snr.rwRegs)
                    {
                        @if (reg.Value.DataType == DataTypeEnum.DOUBLE)
                        {
                            <p class="text-gray-700" onclick="@(() => ChangValueDouble(reg.Key, reg.Value.dVal))"> @reg.Key : @reg.Value</p>
                        }
                        else if (reg.Value.DataType == DataTypeEnum.INTEGER)
                        {
                            <p class="text-gray-700" onclick="@(() => ChangValueInt(reg.Key, reg.Value.iVal))"> @reg.Key : @reg.Value</p>
                        }
                    }
                }
            </div>
        </div>
    </div>
</body>


